package remind;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.lang.model.element.Element;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.StyledDocument;

public class Translit {

    private static final Map<Character, String> charUkrMap = new HashMap<Character, String>();
    private static final Map<String, Character> charEngMap = new HashMap<String, Character>();

    static {
        charUkrMap.put('А', "A");
        charUkrMap.put('Б', "B");
        charUkrMap.put('В', "W");
        charUkrMap.put('Г', "H");
        charUkrMap.put('Ґ', "G");
        charUkrMap.put('Д', "D");
        charUkrMap.put('Е', "E");
        charUkrMap.put('Є', "Ye");
        charUkrMap.put('Ж', "Zh");
        charUkrMap.put('З', "Z");
        charUkrMap.put('І', "I");
        charUkrMap.put('Ї', "Yi");
        charUkrMap.put('И', "Y");
        charUkrMap.put('Й', "J");
        charUkrMap.put('К', "K");
        charUkrMap.put('Л', "L");
        charUkrMap.put('М', "M");
        charUkrMap.put('Н', "N");
        charUkrMap.put('О', "O");
        charUkrMap.put('П', "P");
        charUkrMap.put('Р', "R");
        charUkrMap.put('С', "S");
        charUkrMap.put('Т', "T");
        charUkrMap.put('У', "U");
        charUkrMap.put('Ф', "F");
        charUkrMap.put('Х', "Kh");
        charUkrMap.put('Ц', "Ts");
        charUkrMap.put('Ч', "Ch");
        charUkrMap.put('Ш', "Sh");
        charUkrMap.put('Щ', "Shch");
        charUkrMap.put('Ю', "Yu");
        charUkrMap.put('Я', "Ya");
        charUkrMap.put('а', "a");
        charUkrMap.put('б', "b");
        charUkrMap.put('в', "w");
        charUkrMap.put('г', "h");
        charUkrMap.put('ґ', "g");
        charUkrMap.put('д', "d");
        charUkrMap.put('е', "e");
        charUkrMap.put('є', "ye");
        charUkrMap.put('ж', "zh");
        charUkrMap.put('з', "z");
        charUkrMap.put('и', "y");
        charUkrMap.put('і', "i");
        charUkrMap.put('ї', "yi");
        charUkrMap.put('й', "j");
        charUkrMap.put('к', "k");
        charUkrMap.put('л', "l");
        charUkrMap.put('м', "m");
        charUkrMap.put('н', "n");
        charUkrMap.put('о', "o");
        charUkrMap.put('п', "p");
        charUkrMap.put('р', "r");
        charUkrMap.put('с', "s");
        charUkrMap.put('т', "t");
        charUkrMap.put('у', "u");
        charUkrMap.put('ф', "f");
        charUkrMap.put('х', "kh");
        charUkrMap.put('ц', "ts");
        charUkrMap.put('ч', "ch");
        charUkrMap.put('ш', "sh");
        charUkrMap.put('щ', "shch");
        charUkrMap.put('ь', "'");
        charUkrMap.put('ю', "yu");
        charUkrMap.put('я', "ya");

        charEngMap.put("A", 'А');
        charEngMap.put("B", 'Б');
        charEngMap.put("W", 'В');
        charEngMap.put("V", 'В');
        charEngMap.put("H", 'Г');
        charEngMap.put("G", 'Г');
        charEngMap.put("D", 'Д');
        charEngMap.put("E", 'Е');
        charEngMap.put("Ye", 'Є');
        charEngMap.put("Ie", 'Є');
        charEngMap.put("Je", 'Є');
        charEngMap.put("Zh", 'Ж');
        charEngMap.put("Z", 'З');
        charEngMap.put("I", 'І');
        charEngMap.put("Y", 'И');
        charEngMap.put("Yi", 'Ї');
        charEngMap.put("Ji", 'Ї');
        charEngMap.put("J", 'Й');
        charEngMap.put("K", 'К');
        charEngMap.put("L", 'Л');
        charEngMap.put("M", 'М');
        charEngMap.put("N", 'Н');
        charEngMap.put("O", 'О');
        charEngMap.put("P", 'П');
        charEngMap.put("R", 'Р');
        charEngMap.put("S", 'С');
        charEngMap.put("T", 'Т');
        charEngMap.put("U", 'У');
        charEngMap.put("F", 'Ф');
        charEngMap.put("Kh", 'Х');
        charEngMap.put("Ts", 'Ц');
        charEngMap.put("C", 'Ц');
        charEngMap.put("Ch", 'Ч');
        charEngMap.put("Sh", 'Ш');
        charEngMap.put("Shch", 'Щ');
        charEngMap.put("Yu", 'Ю');
        charEngMap.put("Ju", 'Ю');
        charEngMap.put("Ya", 'Я');
        charEngMap.put("Ja", 'Я');
        charEngMap.put("a", 'а');
        charEngMap.put("b", 'б');
        charEngMap.put("w", 'в');
        charEngMap.put("v", 'в');
        charEngMap.put("h", 'г');
        charEngMap.put("g", 'г');
        charEngMap.put("d", 'д');
        charEngMap.put("e", 'е');
        charEngMap.put("ye", 'є');
        charEngMap.put("je", 'є');
        charEngMap.put("ie", 'є');
        charEngMap.put("zh", 'ж');
        charEngMap.put("z", 'з');
        charEngMap.put("y", 'и');
        charEngMap.put("i", 'і');
        charEngMap.put("yi", 'ї');
        charEngMap.put("ji", 'ї');
        charEngMap.put("j", 'й');
        charEngMap.put("k", 'к');
        charEngMap.put("l", 'л');
        charEngMap.put("m", 'м');
        charEngMap.put("n", 'н');
        charEngMap.put("o", 'о');
        charEngMap.put("p", 'п');
        charEngMap.put("r", 'р');
        charEngMap.put("s", 'с');
        charEngMap.put("t", 'т');
        charEngMap.put("u", 'у');
        charEngMap.put("f", 'ф');
        charEngMap.put("kh", 'х');
        charEngMap.put("ts", 'ц');
        charEngMap.put("c", 'ц');
        charEngMap.put("ch", 'ч');
        charEngMap.put("sh", 'ш');
        charEngMap.put("shch", 'щ');
        charEngMap.put("'", 'ь');
        charEngMap.put("yu", 'ю');
        charEngMap.put("ju", 'ю');
        charEngMap.put("ya", 'я');
        charEngMap.put("ja", 'я');
        charEngMap.put("\"", '\'');

    }

    public StyledDocument ukr2eng(StyledDocument doc, int start, int end) {
        if (start == end) {
            start = 0;
            end = doc.getLength();
        }
        for (int i = start; i < end; i++) {
            try {
                String charFromMap = charUkrMap.get(doc.getText(i, 1).charAt(0));
                if (charFromMap != null) {
                    doc.insertString(i + 1, charFromMap.toString(), doc.getCharacterElement(i).getAttributes());
                    doc.remove(i, 1);
                }
            } catch (BadLocationException ex) {
                Logger.getLogger(Translit.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return doc;
    }

    public StyledDocument eng2ukr(StyledDocument doc, int start, int end) {
        if (start == end) {
            start = 0;
            end = doc.getLength();
        }
        for (int i = start; i < end; i++) {
            Character charFromMap = null;
            int del = 1;
            try {
                if (i + 4 <= end) {
                    charFromMap = charEngMap.get(doc.getText(i, 4));//string.substring(i, i + 4));
                    if (charFromMap != null) {
                        i += 3;
                        del = 3;
                    }
                }
                if ((i + 3 <= end) && (charFromMap == null)) {
                    charFromMap = charEngMap.get(doc.getText(i, 3));//ch);
                    if (charFromMap != null) {
                        i += 2;
                        del = 2;
                    }
                }
                if ((i + 2 <= end) && (charFromMap == null)) {
                    charFromMap = charEngMap.get(doc.getText(i, 2));
                    if (charFromMap != null) {
                        i++;
                    }
                }
                if ((i + 1 <= end) && (charFromMap == null)) {
                    charFromMap = charEngMap.get(doc.getText(i, 1));
                }
                if (charFromMap != null) {

                    doc.insertString(i + 1, charFromMap.toString(), doc.getCharacterElement(i).getAttributes());
                    doc.remove(i - del + 1, del);


                }
            } catch (BadLocationException ex) {
                Logger.getLogger(Translit.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return doc;
    }
}
